package com.cjm.algorithm.leetcode;

import com.cjm.algorithm.TreeNode;

public class SumOfLeftLeaves404 {
    public static int sumOfLeftLeaves(TreeNode root) {

        return getSum(new SumOfLeftLeavesHelp(root, "null"));
    }

    private static int getSum(SumOfLeftLeavesHelp root) {
        if (root.getTreeNode() == null) return 0;
        int l = getSum(new SumOfLeftLeavesHelp(root.getTreeNode().left, "left"));
        int r = getSum(new SumOfLeftLeavesHelp(root.getTreeNode().right, "right"));
        boolean isLef = false;
        if (root.getTreeNode().left == null && root.getTreeNode().right == null) {
            isLef = true;
        }
        int self = root.direct.equals("left") && isLef ?root.getTreeNode().val:0;
        return l+r+self;
    }

    static class SumOfLeftLeavesHelp {
        private TreeNode treeNode;
        private String direct;

        public SumOfLeftLeavesHelp(TreeNode treeNode, String direct) {
            this.treeNode = treeNode;
            this.direct = direct;
        }

        public TreeNode getTreeNode() {
            return treeNode;
        }

        public void setTreeNode(TreeNode treeNode) {
            this.treeNode = treeNode;
        }

        public String getDirect() {
            return direct;
        }

        public void setDirect(String direct) {
            this.direct = direct;
        }
    }

    public static void main(String[] args) {
        System.err.println(sumOfLeftLeaves(TreeNode.generatorTreeNode(new Integer[]{3,9,20,null,null,15,7})));
        System.err.println(sumOfLeftLeaves(TreeNode.generatorTreeNode(new Integer[]{1})));
        System.err.println(sumOfLeftLeaves(TreeNode.generatorTreeNode(new Integer[]{1,2,3,4,5})));
    }
}
